<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="消耗积分" prop="expendPoints">
        <el-input
          v-model="queryParams.expendPoints"
          placeholder="请输入消耗积分"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="每日抽奖上限" prop="maxNumber">
        <el-input
          v-model="queryParams.maxNumber"
          placeholder="请输入每日抽奖上限"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="中奖概率" prop="probabilityOfWinning">
        <el-input
          v-model="queryParams.probabilityOfWinning"
          placeholder="请输入中奖概率"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="每人每日抽奖次数" prop="prizeDrawNumber">
        <el-input
          v-model="queryParams.prizeDrawNumber"
          placeholder="请输入每人每日抽奖次数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索
        </el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
        >重置
        </el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:prizeDrawRule:add']"
        >新增
        </el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:prizeDrawRule:edit']"
        >修改
        </el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:prizeDrawRule:remove']"
        >删除
        </el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:prizeDrawRule:export']"
        >导出
        </el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="prizeDrawRuleList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="主键" align="center" prop="id"/>
      <el-table-column label="消耗积分" align="center" prop="expendPoints"/>
      <el-table-column label="每日抽奖上限" align="center" prop="maxNumber"/>
      <el-table-column
        label="中奖概率"
        align="center"
        prop="probabilityOfWinning"
      />
      <el-table-column
        label="每人每日抽奖次数"
        align="center"
        prop="prizeDrawNumber"
      />
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:prizeDrawRule:edit']"
          >修改
          </el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:prizeDrawRule:remove']"
          >删除
          </el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改抽奖规则对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="1000px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="200px">
        <el-form-item label="消耗积分" prop="expendPoints">
          <el-input v-model="form.expendPoints" placeholder="请输入消耗积分"/>
        </el-form-item>
        <el-form-item label="每日抽奖上限" prop="maxNumber">
          <el-input v-model="form.maxNumber" placeholder="请输入每日抽奖上限"/>
        </el-form-item>
        <el-form-item label="中奖概率%" prop="probabilityOfWinning">
          <el-input
            v-model="form.probabilityOfWinning"
            placeholder="请输入中奖概率"
          />
        </el-form-item>
        <el-form-item label="每人每日抽奖次数" prop="prizeDrawNumber">
          <el-input
            v-model="form.prizeDrawNumber"
            placeholder="请输入每人每日抽奖次数"
          />
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="奖池生效时间" prop="prizeDrawStartTime">
              <el-date-picker clearable
                              v-model="form.prizeDrawStartTime"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择奖池生效时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="奖池结束时间" prop="prizeDrawEndTime">
              <el-date-picker clearable
                              v-model="form.prizeDrawEndTime"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择奖池结束时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="奖池类型" prop="prizeDrawType">
          <el-select v-model="form.prizeDrawType" placeholder="请选择奖池">
            <el-option
              v-for="dict in dict.type.prize_draw_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-divider content-position="center">商品信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleAddProductInfo"
            >添加
            </el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="handleDeleteProductInfo"
            >删除
            </el-button
            >
          </el-col>
        </el-row>
        <el-table
          :data="productInfoList"
          @selection-change="handleProductInfoSelectionChange"
          ref="productInfo"
        >
          <el-table-column type="selection" width="55" align="right"/>
          <el-table-column label="主键" width="55" align="center" prop="id"/>
          <el-table-column label="商品名称" align="center" prop="productName"/>
          <el-table-column
            label="商品金额"
            align="center"
            prop="productAmount"
          />
          <el-table-column
            label="中奖概率"
            width="80"
            align="center"
            prop="probabilityOfWinning"
          >
            <template #default="scope">
              <el-input
                v-model="scope.row.probabilityOfWinning"
                placeholder="请输入该商品中间概率"
              />
            </template>
          </el-table-column>
          <el-table-column label="商品类型" align="center" prop="productType">
            <template slot-scope="scope">
              <dict-tag
                :options="dict.type.product_type"
                :value="scope.row.productType"
              />
            </template>
          </el-table-column>
          <el-table-column
            label="商品数量"
            width="100"
            align="center"
            prop="productNumber"
          >
            <template #default="scope">
              <el-input
                v-model="scope.row.productNumber"
                placeholder="请输入该商品的数量"
              />
            </template>
          </el-table-column>
          <el-table-column
            label="中奖人ID"
            align="center"
            prop="predeterminePhone"
          >
            <template #default="scope">
              <el-input
                v-model="scope.row.predeterminePhone"
                placeholder="请输入中奖人ID"
              />
            </template>
          </el-table-column>
        </el-table>
        <el-form-item label="备注" prop="remark" class="input-left">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog
      :title="title"
      :visible.sync="open1"
      width="1000px"
      append-to-body
    >
      <el-table
        v-loading="loading"
        :data="productList"
        @selection-change="handleProductSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column label="主键" align="center" prop="id"/>
        <el-table-column label="商品名称" align="center" prop="productName"/>
        <el-table-column label="商品金额" align="center" prop="productAmount"/>
        <el-table-column label="商品类型" align="center" prop="productType">
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.product_type"
              :value="scope.row.productType"
            />
          </template>
        </el-table-column>
        <el-table-column label="出售类型" align="center" prop="productType">
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.conversion_type"
              :value="scope.row.conversionType"
            />
          </template>
        </el-table-column>
        <el-table-column label="商品积分" align="center" prop="productPoints"/>
        <el-table-column
          label="商品数量"
          align="center"
          prop="productQuantity"
        />
        <el-table-column label="商品描述" align="center" prop="productRemark"/>
        <el-table-column label="备注" align="center" prop="remark"/>
      </el-table>
      <pagination
        v-show="productListTotal > 0"
        :total="productListTotal"
        :page.sync="productListQueryParams.pageNum"
        :limit.sync="productListQueryParams.pageSize"
        @pagination="productListGetList"
      />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitProductForm">确 定</el-button>
        <el-button @click="cancelProduct">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listPrizeDrawRule,
  getPrizeDrawRule,
  delPrizeDrawRule,
  addPrizeDrawRule,
  updatePrizeDrawRule,
} from "@/api/system/prizeDrawRule";
import {
  listProductInfo,
  getProductInfo,
  delProductInfo,
  addProductInfo,
  updateProductInfo,
} from "@/api/system/productInfo";
import item from "@/layout/components/Sidebar/Item.vue";
import Treeselect from "@riophae/vue-treeselect";

export default {
  name: "PrizeDrawRule",
  components: {Treeselect},
  dicts: ["product_type", "conversion_type", "prize_draw_type"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 抽奖规则表格数据
      prizeDrawRuleList: [],
      productInfoList: [],
      // 子表选中数据
      checkedProductInfo: [],
      // 子表选中数据
      checkedProductInfoList: [],
      productList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open1: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        expendPoints: null,
        maxNumber: null,
        probabilityOfWinning: null,
        prizeDrawNumber: null,
        prizeDrawType: null,
        prizeDrawStartTime: null,
        prizeDrawEndTime: null,
      },
      productListTotal: 0, //积分抽奖总条数
      productListQueryParams: {
        pageNum: 1,
        pageSize: 10,
        conversionType: 2, //出售类型  2：积分抽奖   1：积分兑换
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询抽奖规则列表 */
    getList() {
      this.loading = true;
      listPrizeDrawRule(this.queryParams).then((response) => {
        this.prizeDrawRuleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    productListGetList() {
      this.loading = true;
      this.handleAddProductInfo();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮
    cancelProduct() {
      this.open1 = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        expendPoints: null,
        maxNumber: null,
        probabilityOfWinning: null,
        prizeDrawNumber: null,
        prizeDrawType: null,
        prizeDrawStartTime: null,
        prizeDrawEndTime: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.productInfoList = [];
      this.open = true;
      this.title = "添加抽奖规则";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getPrizeDrawRule(id).then((response) => {
        this.form = response.data;
        this.productInfoList = response.data.productInfoList;
        this.open = true;
        this.title = "修改抽奖规则";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            this.form.productInfoList = this.productInfoList;
            updatePrizeDrawRule(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.productInfoList = this.productInfoList;
            addPrizeDrawRule(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 提交按钮 */
    submitProductForm() {
      this.open1 = false;
      this.checkedProductInfoList.map((item) =>
        this.productInfoList.push(item)
      );
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除抽奖规则编号为"' + ids + '"的数据项？')
        .then(function () {
          return delPrizeDrawRule(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/prizeDrawRule/export",
        {
          ...this.queryParams,
        },
        `prizeDrawRule_${new Date().getTime()}.xlsx`
      );
    },
    handleProductInfoSelectionChange(selection) {
      this.checkedProductInfo = selection.map((item) => item.id);
    },
    /** 商品图片添加按钮操作 */
    handleAddProductInfo() {
      this.open1 = true;
      listProductInfo(this.productListQueryParams).then((response) => {
        this.productList = response.rows;
        this.productListTotal = response.total;
        this.loading = false;
      });
    } /** 商品图片删除按钮操作 */,
    handleDeleteProductInfo() {
      if (this.checkedProductInfo.length == 0) {
        this.$modal.msgError("请先选择要删除的奖品数据");
      } else {
        const productInfoList = this.productInfoList;
        const checkedProductInfo = this.checkedProductInfo;
        this.productInfoList = productInfoList.filter(function (item) {
          return checkedProductInfo.indexOf(item.id) == -1;
        });
      }
      console.log(this.productInfoList);
    },
    // 多选框选中数据
    handleProductSelectionChange(selection) {
      this.checkedProductInfoList = selection;
    },
  },
};
</script>
<style lang="scss" scoped>
.input-left {
  display: flex;

  ::v-deep.el-form-item__label {
    width: auto !important;
  }

  ::v-deep.el-form-item__content {
    margin-left: 0px !important;
    flex: 1;
  }
}
</style>
